<?php include "db.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  

<title>User Management System</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>  
<body>  
<div id="main">
<?php
if(!empty($_POST['email']) && !empty($_POST['password']))
{
	if(checkEmail($_POST['email'] && checkPassword($_POST['password'])) {
		$email = mysqli_real_escape_string($mysqli,$_POST['email']);
		$password = mysqli_real_escape_string($mysqli,$_POST['password']);
		$permission = intval($_POST['permission']);
		$firstname = mysqli_real_escape_string($mysqli,$_POST['firstname']);
		$lastname = mysqli_real_escape_string($mysqli,$_POST['lastname']);
		
		$query = "SELECT * FROM users WHERE Email = '".$email."'";
		$data = $mysqli->query($query);   
		$row = $data->fetch_array(MYSQLI_ASSOC);
		 
		if(count($row) != 0)
		{
			echo "<h1>Error</h1>";
			echo "<p>Sorry, that email is already registered. Please go back and try again.</p>";
		}
		else
		{
			$query = "INSERT INTO users (Email, Password, PermissionLevel,FirstName,LastName,Chapter_Id) VALUES('".$email."', '".$password."', '".$permission."', '".$firstname."', '".$lastname."', 0)";
			$registerquery = $mysqli->query($query);
			if($registerquery)
			{
				echo "<h1>Success</h1>";
				echo "<p>Your account was successfully created. Please <a href=\"index.php\">click here to login</a>.</p>";
				echo "Query: $query executed\n"; 
			}
			else
			{
				echo "<h1>Error</h1>";
				echo "<p>Sorry, your registration failed. Please <a href=\"register.php\">click here to try again</a>.</p>";
			}    	
		}
	}
	elseif(!checkEmail($_POST['email'])) {
		echo "<h1>Error</h1>";
		echo "<p>Please input a valid email address to register. Please <a href=\"register.php\">click here to try again</a>.</p>";
	}
	else {
		echo "<p>Please input a valid password to register. Please <a href=\"register.php\">click here to try again</a>.</p>";
	}
}
else
{
	?>
    
   <h1>Register</h1>
    
   <p>Please enter your details below to register.</p>
   <p>A password must be at least 8 characters in length and contain both letters and numbers.</p>
    
	<form method="post" action="register.php" name="registerform" id="registerform">
	<fieldset>
		<label for="email">Email:</label><input type="text" name="email" id="email" /><br />
		<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
		<label for="firstname">First Name:</label><input type="text" name="firstname" id="firstname" /><br />
		<label for="lastname">Last Name:<input type="text" name="lastname" id="lastname" /><br />
		<input type="radio" name="permission" value="0" />0<br />
		<input type="radio" name="permission" value="1" />1<br />
		<input type="submit" name="register" id="register" value="Register" />
	</fieldset>
	</form>
    
    <?php
}
?>

</div>
</body>
</html>